from share import *


def get_all_txs():
  db = get_db()
  cursor = db.cursor()
  sql = f"select hash from big_tx.big_tx"
  cursor.execute(sql)
  lines = cursor.fetchall()

  return [line[0] for line in lines]

# def get_to_addr_of_tx(tx_hash: str):
#   tx_info = w3.eth.get_transaction(tx_hash)
#   return tx_info['to']

# def insert_to_addr_by_tx(tx_hash: str, to_addr: str):
#   db = get_db()
#   cursor = db.cursor()
#   sql = f'update big_tx.big_tx set to_addr="{to_addr}" where hash="{tx_hash}"'
#   cursor.execute(sql)
#   db.commit()

if __name__ == '__main__':
  all_txs = get_all_txs()
  for tx in all_txs:
    index = all_txs.index(tx)
    print(f'[{index}] insert for: {tx}')
    
    tx_info = w3.eth.get_transaction(tx)
    from_addr = tx_info['from']
    to_addr = tx_info['to']
    from_balance = w3.eth.get_balance(from_addr) / 10**18
    if to_addr is None:
      to_balance = 0
    else:
      to_balance = w3.eth.get_balance(to_addr) / 10**18

    db = get_db()
    cursor = db.cursor()
    sql = f'update big_tx.big_tx set from_balance={from_balance}, to_balance={to_balance} where hash="{tx}" and from_balance is null'
    cursor.execute(sql)
    db.commit()
    db.close()
  pass